package com.ggqq.day330;

import java.util.HashMap;
import java.util.Map;

public class SubStr {
    public static void main(String[] args) {
        String s = "pwwkew";
        SubStr subStr = new SubStr();
        System.out.println(subStr.lengthOfLongestSubstring(s));
    }
    //滑动窗口
    public int lengthOfLongestSubstring(String s){
        int n = s.length(),ans =0;
        Map<Character,Integer> map = new HashMap<>();
        for(int end = 0,start = 0;end<n;end++){
            char alpha = s.charAt(end);  //取后指针的字符
            if(map.containsKey(alpha)){ //包含重复的
                start = Math.max(map.get(alpha),start);//更新前指针
            }
            ans = Math.max(ans,end-start+1);
            map.put(s.charAt(end),end+1);//不包含重复的
        }
        return ans;
    }
}
